﻿using Bricks.Business.Models.Account;
using Bricks.Business.Service.Helper.Ioc;
using Bricks.Business.Service.Impl;
using Bricks.Component.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using System.Web;

namespace Bricks.Business.Service.CommonSupport
{
    /// <summary>
    /// 系统数据（单件实例）
    /// </summary>
    //[Export]
    public class SystemData
    {
        //[Import]
        //public IUserInfoService UserService { get; set; }

        #region 单件实体
        private readonly static SystemData instance = new SystemData();

        //private UserInfo currentUser;

        public static SystemData Instance
        {
            get
            {
                return instance;
            }
        }
        #endregion

        public SystemData() { }

        /// <summary>
        /// 当前登录用户名
        /// </summary>
        public string CurrentUserName
        {
            get
            {
                return HttpContext.Current.User.Identity.Name;
            }
        }

        /// <summary>
        /// 用户信息（当前登录用户信息）
        /// </summary>
        //public UserInfo CurrentUser { get; set; }

        /// <summary>
        /// 用户信息（当前登录用户信息）
        /// </summary>
        public UserInfo CurrentUser
        {
            get
            {
                if (String.IsNullOrEmpty(CurrentUserName))
                    return null;
                //if (currentUser == null)
                //{
                //return UserService.GetUserInfoByName(CurrentUserName);
                //var container = HttpContext.Current.Application["Container"] as CompositionContainer;
                //var userService = container.GetExportedValueOrDefault<IUserInfoService>();

                DirectoryCatalog catalog = new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath);
                MefDependencySolver mef = new MefDependencySolver(catalog);
                var userService = mef.GetService(typeof(IUserInfoService)) as IUserInfoService;
                return userService.GetUserInfoByName(CurrentUserName);
                //}
                //return currentUser;
            }
        }
    }
}
